<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);

require_once BASEPATH . '../excel/PHPExcel/IOFactory.php';

define ("FILEPATH", dirname(__FILE__). "/../../../upload/");

class Excel {
	var $filePath;
	var $fileName;
	var $template;
	var $highestColumn;
	var $highestColumnIndex;
	var $highestRow;
	
	var $minCol;
	var $maxCol;
	var $minRow;
	var $maxRow;
	
	function __construct(){		
		//$this->fileName = 'template_import_new2.xlsx';
		//$this->activeSheet = "nhaphang";
	}

    function test($timestamp){
		// $difference = time() - $timestamp;
		// $periods = array("second", "minute", "hour", "day", "week", "month", "years", "decade");
		// $lengths = array("60","60","24","7","4.35","12","10");
		// for($j = 0; $difference >= $lengths[$j]; $j++)
		// $difference /= $lengths[$j];
		// $difference = round($difference);
		// if($difference != 1) $periods[$j].= "s";
		// $text = "$difference $periods[$j] ago";
		// return $text;
		
		//echo "hi";
	}
	
	function get_list_file(){
		return $this->file->get_dir_file_info( FILEPATH );
	}
	
	function set_file($file_name) {
		$this->fileName = $file_name;
	}
	
	function set_sheet($sheet_name) {
		$this->activeSheet = $sheet_name;
	}
	
	function display(){
		$objReader = PHPExcel_IOFactory::createReader('Excel2007'); 
		$objReader->setReadDataOnly(true); 
		$this->objPHPExcel = $objReader->load( FILEPATH . $this->fileName ); 

		$this->objWorksheet = $this->objPHPExcel->getSheetByName( $this->activeSheet );
		$this->highestColumn = $this->objWorksheet->getHighestColumn();
		$this->highestRow = $this->objWorksheet->getHighestRow();		
		$this->highestColumnIndex = PHPExcel_Cell::columnIndexFromString($this->highestColumn);
		
		$range = $this->get_sheet_range();
		$this->set_sheet_range($range[0], $range[1]);
		//$this->display_sheet();
	}
	
	function get_sheet_range(){
		for ($i=1; $i<=$this->highestRow; $i++){
			for ($j=0; $j<$this->highestColumnIndex; $j++){
				$cell = $this->objWorksheet->getCellByColumnAndRow($j, $i);
				
				if ( $cell->getValue() == $this->template['first_row']['begin'] ) {
					//$_cell = $this->objWorksheet->getCellByColumnAndRow($j, $i+2);
					//$coor[] = $_cell->getCoordinate();
					$this->minCol = $j;
					$this->minRow = $i;
				}
				if ( $cell->getValue() == $this->template['last_row']['end'] ){
					//$_cell = $this->objWorksheet->getCellByColumnAndRow($j, $i-1);
					//$coor[] = $_cell->getCoordinate();
					$this->maxCol = $j;
					$this->maxRow = $i;
				}
			}
		}
		//return $coor;
	}
	
	/**
	 * @param from string Eg: A:5
	 * @param to string Eg: M:13
	 */
	function set_sheet_range($from, $to){
		echo $from. " ". $to;
		$partA = explode(":", $from);
		$partB = explode(":", $to);
		$coor['from_row'] = PHPExcel_Cell::columnIndexFromString($partA[0]);
		$coor['to_row'] = PHPExcel_Cell::columnIndexFromString($partB[0]);
	}
	
	function set_template() {
		$this->template = array(
			'first_row' => array(
				'begin' => 'BEGIN_FIRST_ROW',
				'end'	=> 'END_FIRST_ROW'
			),
			'last_row'	=> array(
				'begin' => 'BEGIN_LAST_ROW',
				'end'	=> 'END_LAST_ROW'
			)
		);
	}
	
	function display_sheet(){
		$this->aImportFields = array(
			1 => 'date_add', // Import Date
			2 => 'date_buy', // Buy Date
			3 => 'barcode', // Product Code
			4 => 'id_category_default', // Product Type
			5 => 'id_manufacturer', //
			6 => 'id_supplier',
			7 => 'reference',
		);
		for ($i=$this->minRow; $i<=$this->maxRow; $i++){
			for ($j=$this->minCol; $j<$this->maxCol; $j++) {
				
				//$cell = $this->objWorksheet->getCellByColumnAndRow($j, $i);
				//$cellValue = $cell->getValue();
				//$cellValue = $cell->getCalculatedValue();
				//echo $this->formatDate($this->dateToTimestamp($cellValue)). "<br />";
				//echo "[$i,$j] => ". $cellValue. "<br>";
			}
		}
	}
	
}